package view.cms.desktop
{
    import flash.events.*;
    import flash.filters.*;
    import flash.geom.*;
    
    import model.ModelLocator;
    
    import mx.collections.XMLListCollection;
    import mx.controls.*;
    import mx.events.*;
    import mx.managers.*;
    
    import view.cms.desktop.events.*;

    public class DrawingShape extends BackgroundObject
    {
		public var isArea:Boolean;
		
		private var indicator2:Label;
		private var indicator:Label;
		private var subIndicator:Label;
		
		public var properties:XMLListCollection;
		public var propertiesXML:XML;
		private var pid:int;
		
        public function DrawingShape(width:int=100,height:int=100,backColor:uint=0, backAlpha:Number=1, pid:int=-1,isArea:Boolean=false,datum:Object=null,rotatable:Boolean=true)
        {
        	if ( isArea )
        		backColor = 0xbbbbbb;
        	else
        		backColor = 0xffffff;
        	this.pid = pid;
			this.isArea = isArea;
            this.backgroundSolidColor = backColor;
            this.backgroundAlpha = backAlpha;
            this.rotatable = rotatable;
            this.addPoint(0, 0);
            this.addPoint(0, height);
            this.addPoint(width, height);
            this.addPoint(width, 0);
			
			if ( !datum )
			{            
				this.propertiesXML = 
					<root>
						<prop label='pageId' value={this.pid} />
						<prop label='subId' value='-1' />
						<prop label='productType' value='-1' />
						<prop label='label' value='' />
						<prop label='pos' value={this.isArea?-1:0} />
						<prop label='productId' value='-1' />
						<prop label='linkURL' value='' />
						<prop label='linkTitle' value='' />
						<prop label='imageAlt' value='' />
						<prop label='imageSrc' value='' />
						<prop label='title' value='' />
						<prop label='detail' value='' />
						<prop label='isArea' value={this.isArea} />
						
						<prop label='ad_title' value='' />
						<prop label='ad_res' value='' />
						<prop label='ad_link' value='' />
						<prop label='timStart' value='' />
						<prop label='timEnd' value='' />
						<prop label='ad_set_1' value='' />
						<prop label='ad_set_2' value='' />
						<prop label='nextTitle' value='' />
				
						<prop label='subId2' value='-1' />
						<prop label='pos2' value={this.isArea?-1:0} />
						<prop label='action' value='' />
						
						<prop label='id' value='' />
						<prop label='time' value='' />
					</root>
							
			}
			else
			{
				this.propertiesXML = 
					<root>
						<prop label='pageId' value={this.pid} />
						<prop label='subId' value={datum.subId} />
						<prop label='productType' value={datum.productType} />
						<prop label='label' value={datum.label} />
						<prop label='pos' value={datum.pos} />
						<prop label='productId' value={datum.productId} />
						<prop label='linkURL' value={datum.linkURL} />
						<prop label='linkTitle' value={datum.linkTitle} />
						<prop label='imageSrc' value={datum.imageSrc} />
						<prop label='imageAlt' value={datum.imageAlt} />
						<prop label='title' value={datum.title} />
						<prop label='detail' value={datum.detail} />
						<prop label='isArea' value={this.isArea} />
						
						<prop label='ad_title' value={datum.ad_title} />
						<prop label='ad_res' value={datum.ad_res} />
						<prop label='ad_link' value={datum.ad_link} />
						<prop label='timStart' value={datum.ad_time_start} />
						<prop label='timEnd' value={datum.ad_time_end} />
						<prop label='ad_set_1' value={datum.ad_set_1} />
						<prop label='ad_set_2' value={datum.ad_set_2} />
						<prop label='nextTitle' value={datum.next_ad_title} />
				
						<prop label='subId2' value={datum.subId} />
						<prop label='pos2' value={datum.pos} />
						<prop label='action' value='' />
				
						<prop label='id' value={datum.id} />
						<prop label='time' value={datum.time} />
					</root>
				
			}
			this.properties = new XMLListCollection(propertiesXML.prop);
			
			this.properties.addEventListener(CollectionEvent.COLLECTION_CHANGE,onDataChanged);
			
			addEventListener("updateDrawingShape", onUpdateDrawingShape);
        }
		
//  删除字段		
//						<prop label='nextRes' value='' />
//						<prop label='nextLink' value='' />
//						<prop label='nextStart' value='' />
//						<prop label='nextEnd' value='' />
//						<prop label='nextSet_1' value='' />
//						<prop label='nextSet_2' value='' />

//						<prop label='nextRes' value={datum.next_ad_res} />
//						<prop label='nextLink' value={datum.next_ad_link} />
//						<prop label='nextStart' value={datum.next_ad_time_start} />
//						<prop label='nextEnd' value={datum.next_ad_time_end} />
//						<prop label='nextSet_1' value={datum.next_ad_set_1} />
//						<prop label='nextSet_2' value={datum.next_ad_set_2} />
		
		private function onUpdateDrawingShape(event:Event = null):void
		{
			if(propertiesXML.prop.(@label=='action')[0].@value != 'add')
				propertiesXML.prop.(@label=='action')[0].@value = 'update';
			trace("修改条目--------------------------------------------------" +propertiesXML.prop.(@label=='action')[0].@value );
		}
        
        public function setValue(property:String,value:*):void
        {
        	this.propertiesXML.prop.(@label==property).@value = value;
        	
			this.properties.itemUpdated(this.propertiesXML.prop.(@label==property),'@value');
        }
        
		public function getValue(property:String):String
		{
			var str:String = this.propertiesXML.prop.(@label==property).@value;
			return str;
		}
		
		// 当用户改变元素任意属性时
		private function onDataChanged(event:CollectionEvent):void
		{
			var item:Object;
			var i:int;
			
			if(event.kind == CollectionEventKind.UPDATE)
			{
				
				for(i = 0; i <event.items.length; i++)
				{
					item = event.items[i];
					if ( item.source.@label == 'subId' )
					{
						if ( this.isArea )
						{
							//  数据发生改变时，则action属性变成update
							//  因为在CMSSite中propertiesGrid_itemFocusOutHandler ，修改area 的subId值需要按回车才能生效，所以在这里在update一次
							onUpdateDrawingShape();
							DrawingPage(this.parent).refreshSubId();
						}
						
						this.indicator.text = item.source.@value;
					}
					
					if ( this.isArea )
					{
						if ( item.source.@label == 'label' )
							this.indicator2.text = item.source.@value;
					}
					else
					{
						if ( item.source.@label == 'pos' )
							this.indicator2.text = item.source.@value;
							
						if ( item.source.@label == 'label' )
							this.subIndicator.text = item.source.@value;							
					}
				}
			}
		}
        
        override protected function createChildren():void
        {
        	super.createChildren();
        	
        	this.indicator = new Label();
        	this.indicator.width = 100;
        	this.indicator.height = 20;
			//if ( this.isArea )
        	this.indicator.y = 15;
        	this.indicator.text = this.propertiesXML.prop.(@label=='subId').@value;
        	this.indicator.setStyle('fontSize',12);
        	this.indicator.setStyle('fontWeight','bold');
        	this.addChild(this.indicator);

			if ( this.isArea )
			{
	        	this.indicator2 = new Label();
	        	this.indicator2.width = 100;
	        	this.indicator2.height = 20;
	        	this.indicator2.text = this.propertiesXML.prop.(@label=='label').@value;
	        	this.indicator2.setStyle('fontSize',12);
	        	this.indicator2.setStyle('fontWeight','bold');
	        	this.addChild(this.indicator2);
	  		}
	  		else
	  		{
	        	this.subIndicator = new Label();
	        	this.subIndicator.width = 100;
	        	this.subIndicator.height = 20;
	        	this.subIndicator.text = this.propertiesXML.prop.(@label=='label').@value;
	        	this.subIndicator.setStyle('fontSize',12);
	        	this.subIndicator.setStyle('fontWeight','normal');
	        	this.addChild(this.subIndicator);	
	        	
	        	this.indicator2 = new Label();
	        	this.indicator2.width = 100;
	        	this.indicator2.height = 20;
	        	this.indicator2.y = 30;
	        	this.indicator2.text = this.propertiesXML.prop.(@label=='pos').@value;
	        	this.indicator2.setStyle('fontSize',12);
	        	this.indicator2.setStyle('fontWeight','bold');
	        	this.addChild(this.indicator2);	        	        
	  		}
	  		
        }
        
        public function manualResize(w:Number, h:Number):void
        {
            this.x = this.x * (w / this.width);
            this.y = this.y * (h / this.height);
            this.resizePoints(w, h);
        }
        
        override public function cloneShape():DrawingObject
        {
            var shape:DrawingShape = new DrawingShape(this.width,this.height,this.backgroundSolidColor, this.backgroundAlpha, this.pid,this.rotatable);
            shape.alpha = this.alpha;
            var i:int = 0;
            
            while (i < this.numPoints) 
            {
                shape.addPoint(points[i].x, points[i].y);
                i++;
            };
            
            shape.locked = this.locked;
            shape.manualPosition(0, 0, this.angle);
            return  shape;
        }
        
        override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
        {
            graphics.clear();
            graphics.lineStyle(1, 0, 1);
            graphics.beginFill(backgroundSolidColor, this.backgroundAlpha);
            graphics.moveTo(points[0].x, points[0].y);
            var i:int = 0;
            
            while (i < points.length) 
            {
                graphics.lineTo(points[i].x, points[i].y);
                i++;
            };
            
            graphics.lineTo(points[0].x, points[0].y);
            graphics.endFill();
            super.updateDisplayList(unscaledWidth,unscaledHeight);
            this.measure();
        }
        
		public function getLayout():String
		{
			var xml:XML = new XML("<item/>");
			xml.@id = String(propertiesXML.prop.(@label=='id')[0].@value);
			xml.@x = x;
			xml.@y = y;
			xml.@width = width;
			xml.@height =  height;
			return xml.toXMLString();
			
		}
	
    }
}



